package com.gome.spiderkit.client;

import java.util.ArrayList;
import java.util.concurrent.TimeoutException;

import com.google.protobuf.InvalidProtocolBufferException;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;

public class WebPage {
	public static String PAGE_SETTINGS_LOAD_IMAGES_BOOL = "loadImages";
	public static String PAGE_SETTINGS_JS_ENABLED_BOOL = "javascriptEnabled";
	public static String PAGE_SETTINGS_XSS_AUDITING_BOOL = "XSSAuditingEnabled";
	public static String PAGE_SETTINGS_USER_AGENT_STRING = "userAgent";
	public static String PAGE_SETTINGS_LOCAL_ACCESS_REMOTE_BOOL = "localToRemoteUrlAccessEnabled";
	public static String PAGE_SETTINGS_USERNAME_STRING = "userName";
	public static String PAGE_SETTINGS_PASSWORD_STRING = "password";
	public static String PAGE_SETTINGS_MAX_AUTH_ATTEMPTS_INT = "maxAuthAttempts";
	public static String PAGE_SETTINGS_RESOURCE_TIMEOUTMS_INT = "resourceTimeout";
	public static String PAGE_SETTINGS_WEB_SECURITY_ENABLED_BOOL = "webSecurityEnabled";
	public static String PAGE_SETTINGS_JS_CAN_OPEN_WINDOWS_BOOL = "javascriptCanOpenWindows";
	public static String PAGE_SETTINGS_JS_CAN_CLOSE_WINDOWS_BOOL = "javascriptCanCloseWindows";
	
	private long m_ref;
	private String m_ref_str;
	private WebKitMQ m_webkit_mq;
	private int m_load_timeout;
	private int m_delete_timeout;
	private long m_error_code = 0;
	private WebKit m_webkit;
	
	public WebPage()
	{
		this.m_webkit = null;
		this.m_webkit_mq = null;
		this.m_ref = 0;
		this.m_ref_str = null;
		m_load_timeout = 120000;
		m_delete_timeout = 1200000;
	}
	
	public WebPage(long page_ref, WebKitMQ webkit_mq, WebKit webkit)
	{
		this.m_webkit = webkit;
		this.m_webkit_mq = webkit_mq;
		this.m_ref = page_ref;
		this.m_ref_str = String.valueOf(m_ref);
		m_load_timeout = 120000;
		m_delete_timeout = 1200000;
	}
	
	public WebPage(WebKitMQ webkit_mq)
	{
		webkit_mq.setFailed();
		this.m_webkit_mq = webkit_mq;
		this.m_webkit = null;
	}
	
	public void threadConstruct() {
		this.m_webkit_mq.close();
		this.m_webkit_mq = this.m_webkit_mq.copySelfForNewThread();
		this.m_webkit = new WebKit(m_webkit_mq, m_webkit);
	}
	
	public void setErrorCode(long errorCode) {
		m_error_code = errorCode;
	}
	
	public void setWebKit(WebKit wk) {
		this.m_webkit = wk;
	}
	
	public WebKit getWebKit() {
		return this.m_webkit;
	}
	
	public long errorCode() {
		return m_error_code;
	}
	
	public boolean isFailed()
	{
		return m_webkit_mq.failed();
	}
	
	public boolean loadUrl(String url) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		BasicDBList headers = new BasicDBList();
		String body = new String();
		String encode = new String();
		return loadUrl(url,"get",false,m_load_timeout,m_delete_timeout,headers,body,encode);
	}
	
	public boolean loadUrl(String url,String operation,BasicDBList headers,String body,String encode) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		return loadUrl(url,operation,false,m_load_timeout,m_delete_timeout,headers,body,encode);
	}
	
	public boolean loadUrl(String url,String operation,boolean async, int loadTimeout,int deleteTimeout,BasicDBList headers,String body,String encode) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(String.valueOf((!async==true)?1:0));
		argList.add(url);
		argList.add(operation);
		argList.add(headers.toString());
		argList.add(body);
		argList.add(encode);
		argList.add(String.valueOf(loadTimeout));
		if (deleteTimeout != -1)
			argList.add(String.valueOf(deleteTimeout));
		
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_VOID_LOADURL_LONG_REF_STRING_ADDRESS_STRING_OPERATION_QMAP_STRING_STRING_HEADERS_STRING_BODYSTRING_STRING_ENCODING, argList);
		WebPageRef webpageref = new WebPageRef();
		long loadRet = m_webkit_mq.getPageValue(msg,webpageref);
		//System.out.println("===============>loadRet:"+loadRet);
		return loadRet<0?false:true;
	}
	
	public void setHtml(String html, String url) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(html);
		argList.add(url);
		m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_VOID_SETHTML_LONG_REF_STRING_HTML, argList);
	}
	
	public void setting(String name,String setting) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(name);
		argList.add(setting);
		m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_VOID_SETSETTING_LONG_REF_STRING_NAME_STRING_SETTING, argList);
	}
	
	public String getTitle() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_STRING_GETTITLE_LONG_REF, argList);
		return m_webkit_mq.getString(msg);
	}
	
	public String getDomTree() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_STRING_GETDOMTREEDUMP_LONG_REF, argList);
		return m_webkit_mq.getString(msg);
	}
	
	public String getPageSource() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_STRING_GETPAGESOURCE_LONG_REF, argList);
		return m_webkit_mq.getString(msg);
	}
	
	public String getUrl() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_STRING_GETURL_LONG_REF, argList);
		if(msg == null || msg.isEmpty())
			return null;
		return m_webkit_mq.getString(msg);
	}
	
	public void goBack() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_VOID_GOBACK_LONG_REF, argList);
	}
	
	public void goForward() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_VOID_GOFORWARD_LONG_REF, argList);
	}
	
	public boolean refresh() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_LONG_REFRESH_LONG_REF, argList);
		return m_webkit_mq.getLong(msg) == 1?true:false;
	}
	
	public WebElement getElementById(String query) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(query);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_LONG_GETELEMENTBYID_LONG_REF_STRING_QUERY, argList);
		long element_ref = m_webkit_mq.getLong(msg);
		if(element_ref <= 0)
			return null;
		return new WebElement(element_ref,m_ref,m_webkit_mq);
	}
	
	public ArrayList<WebElement> getElementByTagName(String query) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<WebElement> elements = new ArrayList<WebElement>();
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(query);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_LONG_GETELEMENTSBYTAGNAME_LONG_REF_STRING_QUERY, argList);
		ArrayList<Long> list = m_webkit_mq.getLongArrayList(msg);
		for(Long element_ref:list)
		{
			WebElement element = new WebElement(element_ref,m_ref,m_webkit_mq);
			elements.add(element);
		}
		return elements;
	}
	
	public ArrayList<WebElement> getElementByXpath(String query) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<WebElement> elements = new ArrayList<WebElement>();
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(query);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_LONG_GETELEMENTSBYXPATH_LONG_REF_STRING_QUERY, argList);
		ArrayList<Long> list = m_webkit_mq.getLongArrayList(msg);
		for(Long element_ref:list)
		{
			WebElement element = new WebElement(element_ref,m_ref,m_webkit_mq);
			elements.add(element);
		}
		return elements;
	}
	
	public WebElement getElementByCssSelector(String query) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(query);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_LONG_GETELEMENTBYCSSSELECTOR_LONG_REF_STRING_QUERY, argList);
		long element_ref = m_webkit_mq.getLong(msg);
		if(element_ref <= 0)
			return null;
		return new WebElement(element_ref,m_ref,m_webkit_mq);
	}
	
	public ArrayList<WebElement> getElementsByCssSelector(String query) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<WebElement> elements = new ArrayList<WebElement>();
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(query);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_LONG_GETELEMENTSBYCSSSELECTOR_LONG_REF_STRING_QUERY, argList);
		ArrayList<Long> list = m_webkit_mq.getLongArrayList(msg);
		if(list == null)
			return elements;
		for(Long element_ref:list)
		{
			WebElement element = new WebElement(element_ref,m_ref,m_webkit_mq);
			elements.add(element);
		}
		return elements;
	}
	
	public WebElement activeElement() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_LONG_ACTIVEELEMENT, argList);
		long element_ref = m_webkit_mq.getLong(msg);
		if(element_ref <= 0)
			return null;
		return new WebElement(element_ref,m_ref,m_webkit_mq);
	}
	
	//unused
	public long selectFrameByName(String name) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(name);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_LONG_SELECTFRAMEBYNAME_STRING_NAME, argList);
		return m_webkit_mq.getLong(msg);
	}
	
	//unused
	public long selectFrameByIdx(int index) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(String.valueOf(index));
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_LONG_SELECTFRAMEBYIDX_LONG_IDX, argList);
		return m_webkit_mq.getLong(msg);
	}
	
	public String getAlertText(boolean clr) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(String.valueOf(clr));
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_STRING_GETALERTTEXT_LONG_REF_BOOL_CLR, argList);
		return m_webkit_mq.getString(msg);
	}
	
	public void setViewportSize(int w,int h) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(String.valueOf(w));
		argList.add(String.valueOf(h));
		m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_VOID_SETVIEWPORTSIZE_INT_W_INT_H, argList);
	}
	
	public String download(WebElement element, int timeout) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(String.valueOf(element.getRef()));
		argList.add(String.valueOf(timeout));
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_LONG_DOWNLOAD_LONG_REF, argList);
		String ret = m_webkit_mq.getDownloadLinkReturn(msg);
		return ret;
	}
	
	public boolean click(WebElement element) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		return click(element,false,m_load_timeout,m_delete_timeout,null);
	}
	
	public boolean click(WebElement element,boolean openNewPage,WebPage newPage) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		return click(element,openNewPage,m_load_timeout,m_delete_timeout,newPage);
	}
	
	public boolean click(WebElement element,boolean openNewPage,int loadTimeout,int deleteTimeout,WebPage newPage) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		if(element.getPageRef() != m_ref)
			return false;
		
		if(!m_webkit_mq.checkAvailable())
			return false;
		
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(String.valueOf(element.getRef()));
		argList.add("1");
		argList.add(String.valueOf(openNewPage == true ? 1:0));
		argList.add(String.valueOf(loadTimeout));
		argList.add(String.valueOf(deleteTimeout));
		
		String msg = m_webkit_mq.webKitCall(element.getPageRef(), WebKitProtocol.WEBELEMENT_LONG_CLICK_LONG_REF, argList, loadTimeout+WebKitMQ.DEFAULT_TIMEOUT);
		WebPageRef webpageRef = new WebPageRef();
		long ret = m_webkit_mq.getPageValue(msg, webpageRef);
		if(openNewPage)
		{
			WebKitMQ _webkit_mq = this.m_webkit_mq.copySelfForNewThread();
			WebKit _webkit = new WebKit(_webkit_mq, m_webkit);
			
			newPage.setWebKit(_webkit);
			newPage.setRef(webpageRef.getRef());
			newPage.setRefStr(String.valueOf(webpageRef.getRef()));
			newPage.setWebkitMq(_webkit_mq);
		}
		if(ret < 0)
			return false;
		
		return true;
	}
	
	public boolean sendKeys(WebElement element,String type,long keys) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		WebPage page = new WebPage();
		return sendKeys(element,type,keys,false,m_load_timeout,m_delete_timeout,page);
	}
	
	public boolean sendKeys(WebElement element,String type,long keys,boolean openNewPage,WebPage newPage) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		return sendKeys(element,type,keys,openNewPage,m_load_timeout,m_delete_timeout,newPage);
	}
	
	
	public boolean sendKeys(WebElement element,String type,long keys,boolean openNewPage,int loadTimeout,int deleteTimeout,WebPage newPage) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		if(!m_webkit_mq.checkAvailable())
			return false;
		
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(String.valueOf(element.getRef()));
		argList.add(type);
		argList.add(String.valueOf(keys));
		argList.add("1");
		argList.add(String.valueOf(openNewPage == true ? 1: 0));
		argList.add(String.valueOf(loadTimeout));
		argList.add(String.valueOf(deleteTimeout));
		
		String msg = m_webkit_mq.webKitCall(element.getPageRef(), WebKitProtocol.WEBELEMENT_LONG_SENDKEYS_LONG_REF_STRING_TYPE_LONG_KEYS, argList);
		WebPageRef webpageRef = new WebPageRef();
		long ret = m_webkit_mq.getPageValue(msg, webpageRef);
		if(openNewPage)
		{
			WebKitMQ _webkit_mq = this.m_webkit_mq.copySelfForNewThread();
			WebKit _webkit = new WebKit(_webkit_mq, m_webkit);
			
			newPage.setWebKit(_webkit);
			newPage.setRef(webpageRef.getRef());
			newPage.setRefStr(String.valueOf(webpageRef.getRef()));
			newPage.setWebkitMq(_webkit_mq);
		}
		if(ret < 0)
			return false;
		
		return true;
	}
	
	public long getRef() {
		return m_ref;
	}

	public void setRef(long m_ref) {
		this.m_ref = m_ref;
	}

	public String getRefStr() {
		return m_ref_str;
	}

	public void setRefStr(String m_ref_str) {
		this.m_ref_str = m_ref_str;
	}

	public WebKitMQ getWebkitMq() {
		return m_webkit_mq;
	}

	public void setWebkitMq(WebKitMQ m_webkit_mq) {
		this.m_webkit_mq = m_webkit_mq;
	}

	public void setLoadTimeout(int loadTimeout){
		m_load_timeout = loadTimeout;
	}
	
	public int getLoadTimeout(){
		return m_load_timeout;
	}
	
	public boolean setDeleteTimeout(int deleteTimeout) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(String.valueOf(deleteTimeout));
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_LONG_SET_DESTROY_TIME, argList);
		boolean ret = m_webkit_mq.getBoolean(msg);
		if(ret)
			m_delete_timeout = deleteTimeout;
		else
			m_delete_timeout = 0;
		
		return ret;
	}
	
	public int getDeleteTimeout(){
		return m_delete_timeout;
	}
	
	public void destory() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		destory(false);
	}
	
	public void destory(boolean needReleaseWebKit) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBKIT_LONG_DESTROY_LONG_REF, argList);
		
		if (needReleaseWebKit)
			m_webkit_mq.close();
	}
	
	public String getStateInfo() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		String msg = m_webkit_mq.webKitCall(0, WebKitProtocol.WEBKIT_STRING_GETSTATEINFO, argList);
		return m_webkit_mq.getString(msg);
	}
	
	public boolean waitForWebElement(String cssSelector,int timeout) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		if (m_webkit_mq.failed())
			return false;
		
		int tryTime = 200;
		int tryCount = timeout/tryTime;
		int tryMod = timeout%tryTime;
		if(tryMod != 0)
			++tryCount;
		int count = 0;
		while(++count <= tryCount)
		{
			try {
				Thread.sleep(tryTime);
				WebElement element = this.getElementByCssSelector(cssSelector);
				if(element == null)
					continue;
				else
					return true;
						
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	
	public String getDOMTreeSource() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException {
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_STRING_GETDOMTREESOURCE_LONG_REF, argList);
		return m_webkit_mq.getString(msg);
	}
	
	public boolean setCookies(ArrayList<Cookie> cookies) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException {
		CookieJar jar = new CookieJar(cookies);
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(jar.toJson());
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBKIT_LONG_SETCOOKIE_LONG_REF_STRING_COOKIES_STRING_URL, argList);
		return m_webkit_mq.getLong(msg) == 1?true:false;
	}
	
	public ArrayList<Cookie> getCookies() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException {
		ArrayList<String> argList = new ArrayList<String>();
		ArrayList<Cookie> retList = new ArrayList<Cookie>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_STRING_COOKIES_LONG_REF, argList);
		ArrayList<BasicDBObject> cs = m_webkit_mq.getObjArrayList(msg);
		if (cs != null) {
			for ( BasicDBObject c : cs) {
				Cookie cookie = new Cookie(c);
				retList.add(cookie);
			}
		}
		return retList;
	}
	
	public CookieJar getCookieJar() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException {
		CookieJar jar = new CookieJar(getCookies());
		return jar;
	}
	
	public void setFilterRule(ArrayList<String> rules) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException {
		ArrayList<String> argList = new ArrayList<String>();
		BasicDBList listObj = new BasicDBList();
		for (String r : rules) {
			listObj.add(r);
		}
		argList.add(listObj.toString());
		m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_VOID_SETFILTERRULE_STRING_RULES, argList);
	}
	
	public String postData() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException {
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_STRING_POSTDATA_LONG_REF, argList);
		return m_webkit_mq.getString(msg);
	}
	
	public String method() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException {
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_STRING_METHOD_LONG_REF, argList);
		return m_webkit_mq.getString(msg);
	}
	
	public void scroll(int x, int y) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException {
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(String.valueOf(x));
		argList.add(String.valueOf(y));
		m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_VOID_SCROLL, argList);
		return ;
	}
	
	public void mouseMove(WebElement element) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException {
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(String.valueOf(element.getRef()));
		m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_VOID_MOUSEMOVE_LONG_REF, argList);
		return ;
	}
	
	public void mouseMovePos(int x, int y) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException {
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(String.valueOf(x));
		argList.add(String.valueOf(y));
		m_webkit_mq.webKitCall(m_ref, WebKitProtocol.WEBPAGE_VOID_MOUSEMOVE_INT_X_INT_Y, argList);
		return ;
	}
	
	public boolean waitForWebElementInnerXmlChange(String cssSelector,int timeout) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		return waitForWebElementChange(cssSelector,timeout,1);
	}
	
	public boolean waitForWebElementOuterXmlChange(String cssSelector,int timeout) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		return waitForWebElementChange(cssSelector,timeout,2);
	}
	
	//type == 1 innerXml
	//type == 2 outerXml
	public boolean waitForWebElementChange(String cssSelector,int timeout,int type) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		int tryTime = 200;
		int tryCount = timeout/tryTime;
		int tryMod = timeout%tryTime;
		if(tryMod != 0)
			++tryCount;
		int count = 0;
		String prefixContent = "";
		String curContent = "";
		WebElement prefixElement = this.getElementByCssSelector(cssSelector);
		if(prefixElement != null)
		{
			if(type == 1)
				prefixContent = prefixElement.getInnerXml();
			else if(type == 2)
				prefixContent = prefixElement.getOuterXml();
		}
		
		
		while(++count <= tryCount)
		{
			try {
				Thread.sleep(tryTime);
				WebElement element = this.getElementByCssSelector(cssSelector);
				if(element == null)
					continue;
				else
				{
					WebElement curElement = this.getElementByCssSelector(cssSelector);
					if(curElement != null)
					{
						if(type == 1)
							curContent = curElement.getInnerXml();
						else if(type == 2)
							curContent = curElement.getOuterXml();
						if(curContent.compareTo(prefixContent) != 0)
						{
							Thread.sleep(tryTime);
							return true;
						}
					}
				}

			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
}
